package com;

import com.dao.UserDao;
import com.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@ComponentScan
@EnableAspectJAutoProxy
@EnableWebMvc
public class AppConfig {
//    public static void main(String[] args) {
//        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
//        HelloSpring h= context.getBean(HelloSpring.class);
//        h.setUserName("小明");
//        h.show();
//        UserService userService =  context.getBean(UserService.class);
//        boolean flag = userService.login("张三", "123");
//        if (flag) {
//            System.out.println("登录成功");
//        }else {
//            System.out.println("登录失败");
//        }
//        UserDao userDao = context.getBean(UserDao.class);
//        userDao.delete();
//        userDao.insert();
//        userDao.select();
//        userDao.update();
//    }
    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/pages/");//模板文件位置
        viewResolver.setSuffix(".jsp");// 自动解析后缀名
        return viewResolver;
    }
    @Bean
    WebMvcConfigurer createWebMvcConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/static/**").addResourceLocations("/static/");
            }
        };
    }
    @Bean
    CommonsMultipartResolver multipartResolver(){//方法名不能更改
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setDefaultEncoding("UTF-8");
        multipartResolver.setMaxUploadSize(10000000);//最大文件10M
        return multipartResolver;
    }
}